package Framework;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import connection.Connection;

public class LoginFrame extends JFrame implements ActionListener {

	Main main;

	JPanel loginPanel;

	JLabel nameLabel;
	JLabel ipLabel;
	JLabel portLabel;

	JTextField nameField;
	JTextField ipField;
	JTextField portField;

	private JButton submitButton = new JButton("Connect");

	public LoginFrame(Main main) {
		this.main = main;
		loginPanel = new JPanel();
		loginPanel.setLayout(new GridLayout(0, 2));

		nameLabel = new JLabel("Vul hier uw naam in:");
		loginPanel.add(nameLabel);
		nameField = new JTextField("Gast");
		loginPanel.add(nameField);

		ipLabel = new JLabel("IP-adres:");
		loginPanel.add(ipLabel);
		ipField = new JTextField(Main.IP);
		loginPanel.add(ipField);

		portLabel = new JLabel("Poort:");
		loginPanel.add(portLabel);
		portField = new JTextField("" + Main.PORT);
		loginPanel.add(portField);

		submitButton.addActionListener(this);
		loginPanel.add(submitButton);

		add(loginPanel);
		setSize(400, 200);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	private void init() {
		String p = nameField.getText();
		String ip = ipField.getText();
		int port = Integer.parseInt(portField.getText());
		if (!p.equals("") && !ip.equals("")) {
			main.c = new Connection(p, ip, port, main);
		}
		setVisible(false);
		main.gp = new GameSelectFrame(main, p);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == submitButton) {
			init();
			this.setVisible(false);
		}
	}
}